import 'dart:convert';

import 'package:mtf/app/data/OrderType.dart';

UsdRecordEntity usdRecordEntityFromJson(String str) => UsdRecordEntity.fromJson(json.decode(str));

String usdRecordEntityToJson(UsdRecordEntity data) => json.encode(data.toJson());

class UsdRecordEntity {
  String total;
  List<ListUSDElement> list;

  UsdRecordEntity({
    required this.total,
    required this.list,
  });

  factory UsdRecordEntity.fromJson(Map<String, dynamic> json) => UsdRecordEntity(
    total: json["total"],
    list: List<ListUSDElement>.from(json["list"].map((x) => ListUSDElement.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "total": total,
    "list": List<dynamic>.from(list.map((x) => x.toJson())),
  };
}

class ListUSDElement {
  String? deal;
  String? externalId;
  String? login;
  String? dealer;
  String? order;
  String? action;
  String? entry;
  String? reason;
  String? digits;
  String? digitsCurrency;
  String? contractSize;
  String? time;
  String? timeMsc;
  String? symbol;
  String? price;
  String? volume;
  String? volumeExt;
  String? profit;
  String? storage;
  String? commission;
  String? fee;
  String? rateProfit;
  String? rateMargin;
  String? expertId;
  String? positionId;
  String? comment;
  String? profitRaw;
  String? pricePosition;
  String? priceSl;
  String? priceTp;
  String? volumeClosed;
  String? volumeClosedExt;
  String? tickValue;
  String? tickSize;
  String? flags;
  String? gateway;
  String? priceGateway;
  String? modifyFlags;
  String? value;
  String? marketBid;
  String? marketAsk;
  String? marketLast;
  String? actionTxt;
  double? volumeTxt;
  DateTime? timeAt;
  String? entryTxt;
  int? num;

  ListUSDElement({
    required this.deal,
    required this.externalId,
    required this.login,
    required this.dealer,
    required this.order,
    required this.action,
    required this.entry,
    required this.reason,
    required this.digits,
    required this.digitsCurrency,
    required this.contractSize,
    required this.time,
    required this.timeMsc,
    required this.symbol,
    required this.price,
    required this.volume,
    required this.volumeExt,
    required this.profit,
    required this.storage,
    required this.commission,
    required this.fee,
    required this.rateProfit,
    required this.rateMargin,
    required this.expertId,
    required this.positionId,
    required this.comment,
    required this.profitRaw,
    required this.pricePosition,
    required this.priceSl,
    required this.priceTp,
    required this.volumeClosed,
    required this.volumeClosedExt,
    required this.tickValue,
    required this.tickSize,
    required this.flags,
    required this.gateway,
    required this.priceGateway,
    required this.modifyFlags,
    required this.value,
    required this.marketBid,
    required this.marketAsk,
    required this.marketLast,
    required this.actionTxt,
    required this.volumeTxt,
    required this.timeAt,
    required this.entryTxt,
    required this.num,
  });

  factory ListUSDElement.fromJson(Map<String, dynamic> json) => ListUSDElement(
    deal: json["Deal"],
    externalId: json["ExternalID"],
    login: json["Login"],
    dealer: json["Dealer"],
    order: json["Order"],
    action: json["Action"],
    entry: json["Entry"],
    reason: json["Reason"],
    digits: json["Digits"],
    digitsCurrency: json["DigitsCurrency"],
    contractSize: json["ContractSize"],
    time: json["Time"],
    timeMsc: json["TimeMsc"],
    symbol: json["Symbol"],
    price: json["Price"],
    volume: json["Volume"],
    volumeExt: json["VolumeExt"],
    profit: json["Profit"],
    storage: json["Storage"],
    commission: json["Commission"],
    fee: json["Fee"],
    rateProfit: json["RateProfit"],
    rateMargin: json["RateMargin"],
    expertId: json["ExpertID"],
    positionId: json["PositionID"],
    comment: json["Comment"],
    profitRaw: json["ProfitRaw"],
    pricePosition: json["PricePosition"],
    priceSl: json["PriceSL"],
    priceTp: json["PriceTP"],
    volumeClosed: json["VolumeClosed"],
    volumeClosedExt: json["VolumeClosedExt"],
    tickValue: json["TickValue"],
    tickSize: json["TickSize"],
    flags: json["Flags"],
    gateway: json["Gateway"],
    priceGateway: json["PriceGateway"],
    modifyFlags: json["ModifyFlags"],
    value: json["Value"],
    marketBid: json["MarketBid"],
    marketAsk: json["MarketAsk"],
    marketLast: json["MarketLast"],
    actionTxt: json["Action_txt"],
    volumeTxt: json["Volume_txt"]?.toDouble(),
    timeAt: DateTime.parse(json["Time_at"]),
    entryTxt: json["Entry_txt"],
    num: json["num"],
  );

  Map<String, dynamic> toJson() => {
    "Deal": deal,
    "ExternalID": externalId,
    "Login": login,
    "Dealer": dealer,
    "Order": order,
    "Action": action,
    "Entry": entry,
    "Reason": reason,
    "Digits": digits,
    "DigitsCurrency": digitsCurrency,
    "ContractSize": contractSize,
    "Time": time,
    "TimeMsc": timeMsc,
    "Symbol": symbolValues.reverse[symbol],
    "Price": price,
    "Volume": volume,
    "VolumeExt": volumeExt,
    "Profit": profit,
    "Storage": storage,
    "Commission": commission,
    "Fee": fee,
    "RateProfit": rateProfit,
    "RateMargin": rateMargin,
    "ExpertID": expertId,
    "PositionID": positionId,
    "Comment": comment,
    "ProfitRaw": profitRaw,
    "PricePosition": pricePosition,
    "PriceSL": priceSl,
    "PriceTP": priceTp,
    "VolumeClosed": volumeClosed,
    "VolumeClosedExt": volumeClosedExt,
    "TickValue": tickValue,
    "TickSize": tickSize,
    "Flags": flags,
    "Gateway": gateway,
    "PriceGateway": priceGateway,
    "ModifyFlags": modifyFlags,
    "Value": value,
    "MarketBid": marketBid,
    "MarketAsk": marketAsk,
    "MarketLast": marketLast,
    "Action_txt": actionTxt,
    "Volume_txt": volumeTxt,
    "Time_at": timeAt?.toIso8601String(),
    "Entry_txt": entryTxtValues.reverse[entryTxt],
    "num": num,
  };

  List<OrderType> infoMap(){
    List<OrderType> maps=[];
    maps.add(OrderType("交易账号",login??""));
    maps.add(OrderType("交易类型", actionTxt??""));
    maps.add(OrderType("交易品种", symbol??""));
    maps.add(OrderType("交易量(手数)", volumeTxt.toString()??""));
    maps.add(OrderType("交易价格", price??""));
    maps.add(OrderType("开/平仓", entryTxt??""));
    maps.add(OrderType("盈亏", profit??""));
    maps.add(OrderType("手续费", priceTp??""));
    maps.add(OrderType("隔夜利息", storage??""));
    return maps;
  }
}


enum EntryTxt {
  IN,
  OUT
}

final entryTxtValues = EnumValues({
  "in": EntryTxt.IN,
  "out": EntryTxt.OUT
});

enum Symbol {
  XAUUSD_MT
}

final symbolValues = EnumValues({
  "XAUUSD.mt": Symbol.XAUUSD_MT
});

class EnumValues<T> {
  Map<String, T> map;
  late Map<T, String> reverseMap;

  EnumValues(this.map);

  Map<T, String> get reverse {
    reverseMap = map.map((k, v) => MapEntry(v, k));
    return reverseMap;
  }
}
